package com.xin.ruanjian.omsystem.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.xin.ruanjian.omsystem.R;
import com.xin.ruanjian.omsystem.app.MyApplication;
import com.xin.ruanjian.omsystem.bases.BaseActivity;
import com.xin.ruanjian.omsystem.bases.Login;
import com.xin.ruanjian.omsystem.utils.HttpUtils;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.Call;

import static com.xin.ruanjian.omsystem.bases.Contants.TS_PAYMENT;

public class PaymentActivity extends BaseActivity {
    @BindView(R.id.tv_pay)
    Button tvPay;
    @BindView(R.id.iv_rightPic)
    ImageView ivRightPic;
    @BindView(R.id.tv_time_remaining)
    TextView tvTimeRemaining;
    @BindView(R.id.tv_all_price)
    TextView tvAllPrice;
    @BindView(R.id.tv_shop_name)
    TextView tvShopName;
    @BindView(R.id.tv_alipay)
    TextView tvAlipay;
    @BindView(R.id.tv_wechat)
    TextView tvWechat;
    @BindView(R.id.tv_card)
    TextView tvCard;
    @BindView(R.id.iv_alipay)
    ImageView ivAlipay;
    @BindView(R.id.iv_wechat)
    ImageView ivWechat;
    @BindView(R.id.iv_card)
    ImageView ivCard;
    private int select = 0;
    private String orderid;
    private Login login = MyApplication.getLogin();

    @Override
    protected void initContentView(Bundle bundle) {
        setContentView(R.layout.activity_payment);
    }

    @Override
    protected void initData() {
        orderid = getIntent().getStringExtra("orderid");
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initTitle() {
        showDefaultBackIcon();
        setTvCenterTitle("订单支付");
    }

    @Override
    protected void main() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    private void postOrder() {
        showLoadingDialog();
        HttpUtils httpUtils = new HttpUtils(TS_PAYMENT) {
            @Override
            public void onError(Call call, Exception e, int id) {
                showErrorDialog();
            }

            @Override
            public void onResponse(String response, int id) {
                hideLoadingDialog();
                try {
                    JSONObject o = new JSONObject(response);
                    int state = o.getInt("state");
                    if (state == 1) {
                        Toast("付款成功");
                        setResult(1201);
                        finish();
                    } else if (state == 0) {
                        showErrorDialog();
                    } else if (state == 9) {
                        Toast(R.string.token_verification_fails_please_log_in_again);
                        MyApplication.saveLogin(null);
                        startActivity(new Intent(context, LoginActivity.class));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        };
        if (login != null) {
            if (orderid != null) {
                httpUtils.addParam("orderid", orderid);
                httpUtils.addParam("userid", login.getUserId() + "");
                httpUtils.addParam("token", login.getToken());
                httpUtils.addParam("type", select+"");
                httpUtils.clicent();
            } else {
                Toast("订单获取错误");
            }
        } else {
            Toast(R.string.token_verification_fails_please_log_in_again);
            startActivity(new Intent(context, LoginActivity.class));
        }
    }

    @OnClick({R.id.tv_pay, R.id.tv_alipay, R.id.tv_wechat, R.id.tv_card})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_pay:
                postOrder();
                break;
            case R.id.tv_alipay:
                select = 0;
                ivAlipay.setVisibility(View.VISIBLE);
                ivWechat.setVisibility(View.GONE);
                ivCard.setVisibility(View.GONE);
                break;
            case R.id.tv_wechat:
                select = 1;
                ivAlipay.setVisibility(View.GONE);
                ivWechat.setVisibility(View.VISIBLE);
                ivCard.setVisibility(View.GONE);
                break;
            case R.id.tv_card:
                select = 2;
                ivAlipay.setVisibility(View.GONE);
                ivWechat.setVisibility(View.GONE);
                ivCard.setVisibility(View.VISIBLE);
                break;
        }
    }
}
